<?php

class BonusBatch extends DataObject implements PermissionProvider {
    private static $singular_name = "Bonus Batch";
    private static $plural_name = "Bonus Batches";

    private static $db = array(
        'Date' => 'Date',
        'Code' => 'Varchar',
        'DateStart' => 'Date',
        'DateEnd' => 'Date',
        'IsActive' => 'Boolean',
        'IsClosed' => 'Boolean'
    );
	
	private static $default_sort = 'Date DESC';

    private static $searchable_fields = array(
        'Code',
        'Date' => array(
			'field' => 'DateField',
			'filter' => 'DateMatchFilter'
		),
        'IsActive',
        'IsClosed'
    );

    private static $summary_fields = array(
        'Code',
        'DateStart.Nice',
        'DateEnd.Nice',
        'IsActive.Nice',
        'IsClosed.Nice'
    );

    static function get_current_batch() {
        $batch = BonusBatch::get()->filter('IsActive', 1)->filter('IsClosed', 0)->filter('DateStart:LessThanOrEqual', date('Y-m-d'))->filter('DateEnd:GreaterThanOrEqual', date('Y-m-d'))->sort('DateStart')->first();
        if(!$batch) {
            $batch = BonusBatch::get()->filter('IsActive', 1)->filter('IsClosed', 0)->sort('DateStart')->first();
        }
        return $batch;
    }

    static function get_current_batch_id() {
        $batch = self::get_current_batch();
        return $batch ? $batch->ID : 0;
    }

    static function get_batch_by_date($date) {
        return BonusBatch::get()->filter('IsActive', 1)->filter('IsClosed', 0)->filter('DateStart:LessThanOrEqual', $date)->filter('DateEnd:GreaterThanOrEqual', $date)->sort('DateStart')->first();
    }

    static function get_batch_id_by_date($date) {
        $batch = self::get_batch_by_date($date);
        return $batch ? $batch->ID : 0;
    }

    public function fieldLabels($includerelations = true) {
        $labels = parent::fieldLabels($includerelations);

        $labels['Code'] = _t('BonusBatch.CODE', 'Code');
		$labels['Date'] = _t('BonusBatch.DATE', 'Date');
        $labels['Date.Nice'] = _t('BonusBatch.DATE', 'Date');
        $labels['DateStart'] = _t('BonusBatch.DATE_START', 'Date Start');
        $labels['DateStart.Nice'] = _t('BonusBatch.DATE_START', 'Date Start');
        $labels['DateEnd'] = _t('BonusBatch.DATE_END', 'Date End');
        $labels['DateEnd.Nice'] = _t('BonusBatch.DATE_END', 'Date End');
        $labels['IsActive'] = _t('BonusBatch.IS_ACTIVE', 'Is Active?');
        $labels['IsActive.Nice'] = _t('BonusBatch.IS_ACTIVE', 'Is Active?');
        $labels['IsClosed'] = _t('BonusBatch.IS_CLOSED', 'Is Closed?');
        $labels['IsClosed.Nice'] = _t('BonusBatch.IS_CLOSED', 'Is Closed?');
        return $labels;
    }

    function canView($member = false) {
        $extended = $this->extendedCan(__FUNCTION__, $member);
        if($extended !== null) {
            return $extended;
        }
        return Permission::check('VIEW_BonusBatch');
    }

    function canEdit($member = false) {
    	if(!$this->exists() && $this->canCreate($member)){
    		return true;
    	}
        $extended = $this->extendedCan(__FUNCTION__, $member);
        if($extended !== null) {
            return $extended;
        }
        return Permission::check('EDIT_BonusBatch');
    }

    function canDelete($member = false) {
        $extended = $this->extendedCan(__FUNCTION__, $member);
        if($extended !== null) {
            return $extended;
        }
        return Permission::check('DELETE_BonusBatch');
    }

    function canCreate($member = false) {
        $extended = $this->extendedCan(__FUNCTION__, $member);
        if($extended !== null) {
            return $extended;
        }
        return Permission::check('CREATE_BonusBatch');
    }

    public function providePermissions() {
        return array(
            'VIEW_BonusBatch' => array(
                'name' => _t('BonusBatch.PERMISSION_VIEW', 'Allow view access right'),
                'category' => _t('BonusBatch.PERMISSIONS_CATEGORY', 'Bonus Batch')
            ),
            'EDIT_BonusBatch' => array(
                'name' => _t('BonusBatch.PERMISSION_EDIT', 'Allow edit access right'),
                'category' => _t('BonusBatch.PERMISSIONS_CATEGORY', 'Bonus Batch')
            ),
            'DELETE_BonusBatch' => array(
                'name' => _t('BonusBatch.PERMISSION_DELETE', 'Allow delete access right'),
                'category' => _t('BonusBatch.PERMISSIONS_CATEGORY', 'Bonus Batch')
            ),
            'CREATE_BonusBatch' => array(
                'name' => _t('BonusBatch.PERMISSION_CREATE', 'Allow create access right'),
                'category' => _t('BonusBatch.PERMISSIONS_CATEGORY', 'Bonus Batch')
            )
        );
    }

}
?>